package com.f21.f21assessoria.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;



import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.sql.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.f21.f21assessoria.controller.UsuarioController;
import com.f21.f21assessoria.model.cadastro.Usuario;

@Transactional(propagation=Propagation.REQUIRED)
//@Repository("CrudDao")
public abstract class CrudDaoImp<T> implements CrudDao<T>, Serializable{
	
	private static final long serialVersionUID = 1L;
	@Autowired
	protected SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void salvar(T t) {
		sessionFactory.getCurrentSession().saveOrUpdate(t);
	}
	
	public void excluir(T t) {
		sessionFactory.getCurrentSession().delete(t);		
	}
	
	public void editar(T obj) {

	}
	
	
	public abstract List<T> listarTodos();
	
	@SuppressWarnings("unchecked")
	public List listarTodos(String classe) {
		try {
		return (List) sessionFactory.getCurrentSession().createQuery("from "+classe).list();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Object> getExecQuery(String query){
		return (List<Object>) sessionFactory.getCurrentSession().createQuery(query).list();
	}
	
	public abstract T buscarPorId(long id);
}